/*
 * @Description: 地图相关获取当前定位类   MapFunction
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-09-03 17:16:42
 * @LastEditors: zhengzhuang
 * @LastEditTime: 2020-09-07 10:16:40
 */
import 'dart:async';
import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:amap_search_fluttify/amap_search_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:C20Driver/utils/permission_handler.dart';
import 'package:permission_handler/permission_handler.dart';

class LocateInfo {
  // Timer _timer;
  // /// 持续获取当前定位
  // Future<void> continuousSendLocation(context, userId) async {
  //   // if (await PermissionHandlerX.position(context)) {
  //     // AmapLocation.listenLocation(mode:LocationAccuracy.High,needAddress: false).listen((location) {
  //       // print('持续设置车主实时位置');
  //       if(_timer != null) {
  //         print('取消定时===============');
  //         _timer.cancel();
  //       }
  //       _timer = Timer.periodic(Duration(seconds: 3), (timer) async {
  //         final location = await AmapLocation.fetchLocation();
  //         print(location);
  //         // return location.latLng.latitude;
  //         Provider.of<PositionInformation>(context, listen: false).setRealtimeDriverPosition(context, location.latLng.latitude, location.latLng.longitude, userId);
  //       });
  //     // });
  //   // }
  // }

  /// 获取当前位置
  static Future<Location> getLocation(BuildContext context) async {
    if (await PermissionHandlerX.position(context)) {
      final location = await AmapLocation.fetchLocation();
      return location;
    } else {
      return null;
    }
  }

  /// 获取定位权限
  static Future permissions() async {
    await PermissionHandler().requestPermissions([PermissionGroup.location]);
  }

  /// 停止连续定位
  void stopLocation(context) async {
    // if (await PermissionHandlerX.position(context)) {
    await AmapLocation.stopLocation();
    // }
  }

  /// 根据经纬度获取具体位置
  Future<String> getLocationInfo(LatLng latlng) async {
    final reGeocodeList = await AmapSearch.searchReGeocode(
      LatLng(latlng.latitude, latlng.longitude),
      radius: 200.0,
    );
    // print('根据经纬度获取具体位置');
    // reGeocodeList.building.then((value) => print(value));
    // print(await reGeocodeList.neighborhood);
    // print(await reGeocodeList.formatAddress);
    // ReGeocode
    // ReGeocode
    // print(await reGeocodeList.neighborhood);
    return await reGeocodeList.formatAddress;
  }
}
